function E = get_q_error2(R, C, qtable)
% calculate E in MME, e.g., error of change LSB
% Input:
% R -> C''- C'
% C -> C''
% qtable
%
% Output:
% E -> Additional Distortion caused by changing any single bit

[P, Q] = size(C);
E = ones(P, Q);
RmC = R.*C;

for i = 1:P
    qi = mod(i, 8);
    if (qi == 0)
        qi = 8;
    end
    for j = 1:Q
        qj = mod(j, 8);
        if (qj == 0)
            qj = 8;
        end
        if (RmC(i,j) <= 0) || (abs(C(i,j)) ~= 1)
            E(i,j) = (1 - 2*abs(R(i,j))) * (qtable(qi, qj).^2);
        end
    end
end
assert(min(E(:)) >= 0);
end
